package org.csu.secondhand.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.secondhand.api.entity.Dealing;
import org.csu.secondhand.api.persistence.DealingMapper;
import org.csu.secondhand.api.service.DealingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("dealingService")
public class DealingServiceImpl implements DealingService {
    @Autowired
    DealingMapper dealingMapper;

    @Override
    public List<Dealing> getAllDealing() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.ge("status", 1);
        queryWrapper.orderByDesc("time");
        List<Dealing> dealingList = dealingMapper.selectList(queryWrapper);
        return dealingList;
    }

    @Override
    public Dealing insertDealing(Dealing dealing) {
        dealingMapper.insert(dealing);
        return dealing;
    }

    @Override
    public List<Dealing> searchDealing(String keyword) {
        QueryWrapper<Dealing> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("itemname", keyword);
        List<Dealing> dealingList = dealingMapper.selectList(queryWrapper);
        return dealingList;
    }

    @Override
    public List<Dealing> getDealingByUser(String user) {
        QueryWrapper<Dealing> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user);
        List<Dealing> dealingList = dealingMapper.selectList(queryWrapper);
        return dealingList;
    }

    @Override
    public Dealing getDealingById(long id) {
        Dealing dealing = dealingMapper.selectById(id);
        return dealing;
    }

    @Override
    public Dealing deleteDealing(long id) {
        Dealing dealing = getDealingById(id);
        dealingMapper.deleteById(id);
        return dealing;
    }

    @Override
    public Dealing editDealing(Dealing dealing) {
        dealingMapper.updateById(dealing);
        return dealing;
    }
}
